import VueRouter from 'vue-router'
import Vue from 'vue'
import Goods from '@/pages/Goods'
import store from '@/store'

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
      return VueRouterPush.call(this, to).catch(err => err)
}

const router = new VueRouter({
    routes:[
        {   
            name:'hom',
            path:'/home',
            component:()=>import("@/pages/Home"),
            meta:{isLogin:true,title:'个人主页'},
            children:[
                {
                    name:'myg',
                    path:'mygoods',
                    component:()=>import("@/pages/Seller/MyGoods"),
                    meta:{isLogin:true,title:'我的商品'},
                },
                {
                    name:'hto',
                    path:'historyOrder',
                    component:()=>import("@/pages/User/HistoryOrder"),
                    meta:{isLogin:true,title:'查询历史订单'},
                },
                {
                    name:'shopcart',
                    path:'shopcart',
                    component:()=>import("@/pages/User/ShopCart"),
                    meta:{isLogin:true,title:'购物车'},
                },
                {
                    name:'favorites',
                    path:'favorites',
                    component:()=>import("@/pages/User/Favorites"),
                    meta:{isLogin:true,title:'收藏夹'},
                },
                {
                    name:'cti',
                    path:'clientsinfo',
                    component:()=>import("@/pages/Seller/ClientsInfo"),
                    meta:{isLogin:true,title:'查询用户信息'},
                },
                {
                    name:'pub',
                    path:'publish',
                    component:()=>import("@/pages/Seller/Publish"),
                    meta:{isLogin:true,title:'发布商品'},
                },
                {
                    name:'myo',
                    path:'myorder',
                    component:()=>import("@/pages/Seller/MyOrder"),
                    meta:{isLogin:true,title:'我的订单'},
                },
                {
                    name:'cat',
                    path:'category',
                    component:()=>import("@/pages/Seller/Category"),
                    meta:{isLogin:true,title:'商品分类管理'},
                },
                {
                    name:'per',
                    path:'personal',
                    component:()=>import("@/pages/Seller/Personal"),
                    meta:{isLogin:true,title:'个人信息管理'},
                    children:[
                        {
                            name:'mes',
                            path:'message',
                            component:()=>import("@/pages/Message"),
                            meta:{isLogin:true,title:'个人信息'},
                        },
                        {
                            name:'Upd',
                            path:'updatapwd',
                            component:()=>import("@/pages/Seller/UpdataPwd"),
                            meta:{isLogin:true,title:'修改密码'},
                        },
                        {
                            name:'udm',
                            path:'updatames',
                            component:()=>import("@/pages/User/UpdateMes"),
                            meta:{isLogin:true,title:'修改客户信息'},
                        },
                    ]
                }
            ]
        },
        {
            name:'log',
            path:'/login',
            component:()=>import("@/pages/Login"),
            meta:{title:'登录界面'},
        },
        {
            name:'reg',
            path:'/register',
            component:()=>import("@/pages/Register"),
            meta:{title:'注册页面'},
        },
        {
            name:'goo',
            path:'/goods',
            component:Goods,
            meta:{title:'购物主页'},
        },
        {
            name:'buy',
            path:'/buy',
            component:()=>import("@/pages/Buy"),
            meta:{title:'购买商品'},
        },
        {
            name:'ord',
            path:'/order',
            component:()=>import("@/pages/Order/Order"),
            meta:{title:'购物订单'},
        },
        {
            name:'che',
            path:'/check',
            component:()=>import("@/pages/Order/Check"),
            meta:{title:'查询订单'},
        },
        {
            name:'asales',
            path:'/aftersales',
            component:()=>import("@/pages/AfterSales"),
            meta:{title:'申请售后'},
        },
        {
            name:'pay',
            path:'/pay',
            component:()=>import("@/pages/Pay/index.vue"),
            meta:{title:'支付订单'},
        },
        {
            name:'paySuccess',
            path:'/paySuccess',
            component:()=>import("@/pages/Pay/paySuccess"),
            meta:{title:'支付成功'},
        },
        {
            name:'odl',
            path:'/OrderList',
            component:()=>import("@/pages/Order/OrderList"),
            meta:{title:'所有订单详情'},
        },
        
        {
            path:'*',
            redirect:'/goods'
        }
    ]
})

//全局前置路由守卫
router.beforeEach((to,from,next)=>{
    if(to.meta.isLogin){//判断是否需要权限
        if(store.state.token){
            next()
        }else{
            alert('您尚未登录,请登录后重试')
            next('/login')
        }
    }else{
        if(to.name==='reg'||to.name==='log'){
            if(store.state.token){
                alert('您已登录,请退出登录后重试')
                next('/goods')
            }else{
                next()
            }
        }else{
            next()
        }
        
    }
})

//全局后置路由守卫
router.afterEach((to,from)=>{
    document.title = to.meta.title || '在线乐器购物系统'
})


export default router;